/**
 * app controller处理
 * 2016-4-15 14:13:09
 */
'use strict';
angular.module('app').controller('AppCtrl', AppCtrl).run(
    ['$rootScope', '$locale', '$state',
        function ($rootScope, $locale, $state) {
            $rootScope.$field = function (name) {
                return $locale.FIELDS[name] || name;
            };
            $rootScope.$msg = function (name) {
                return $locale.MESSAGES[name] || name;
            };
            //404监听事件
            $rootScope.$on('page:notFound', function (event) {
                $state.go('access.404');
            });
            $rootScope.$on('server:error', function (event) {
                $state.go('access.500');
            });
            $rootScope.$on('page:notAuthorized', function (event) {
                $state.go('access.401');
            });
        }
    ]
);
//注入依赖
AppCtrl.$inject = ['$scope', '$rootScope', '$translate', '$localStorage', '$window', '$stateParams', '$sessionService', '$state', '$resource', '$http'];

function AppCtrl($scope, $rootScope, $translate, $localStorage, $window, $stateParams, $sessionService, $state, $resource, $http) {

    var user = {name: "joker", age: "12"};
    $sessionService.setUser(user);
    //添加 ie classes到 html中
    var isIE = !!navigator.userAgent.match(/MSIE/i);
    isIE && angular.element($window.document.body).addClass('ie');
    isSmartDevice($window) && angular.element($window.document.body).addClass('smart');

    //风格默认设置
    $scope.app = {
        name: 'Mybatis生成器',
        version: '1.3.3',
        // for chart colors
        color: {
            primary: '#7266ba',
            info: '#23b7e5',
            success: '#27c24c',
            warning: '#fad733',
            danger: '#f05050',
            light: '#e8eff0',
            dark: '#3a3f51',
            black: '#1c2b36'
        },
        settings: {//主题风格设置
            themeID: 1,
            navbarHeaderColor: 'bg-black',
            navbarCollapseColor: 'bg-white-only',
            asideColor: 'bg-black',
            headerFixed: true,
            asideFixed: false,
            asideFolded: false,
            asideDock: false,
            container: false
        }
    }
    //把应用配置放到本地缓存
    if (angular.isDefined($localStorage.settings)) {
        $scope.app.settings = $localStorage.settings;
    } else {
        $localStorage.settings = $scope.app.settings;
    }
    $scope.$watch('app.settings', function () {
        if ($scope.app.settings.asideDock && $scope.app.settings.asideFixed) {
            //设置dock固定必须先设置header固定
            $scope.app.settings.headerFixed = true;
        }
        //把应用配置放到本地缓存
        $localStorage.settings = $scope.app.settings;
    }, true);

    // 国际化处理
    $scope.lang = {isopen: false};
    //多语言选项
    $scope.langs = {zh_cn: '中文', en: 'English'};
    $scope.selectLang = $scope.langs[$translate.proposedLanguage()] || "English";
    $scope.setLang = function (langKey, $event) {
        // 设置当前使用语言
        $scope.selectLang = $scope.langs[langKey];
        // 设置使用哪种语言
        $translate.use(langKey);
        $scope.lang.isopen = !$scope.lang.isopen;
    };
    //跨页面参数处理
    $rootScope.routeParams = $stateParams;
    //自适应屏幕处理
    function isSmartDevice($window) {
        var ua = $window['navigator']['userAgent'] || $window['navigator']['vendor'] || $window['opera'];
        return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
    }

    //菜单数据获取
    $resource("file/menu.json").get().$promise.then(function (response) {
        $scope.menuItems = response.menusData;
    });

    $http({
        method: 'post',
        url: 'user/findUserInfo.do'
    }).then(function successCallback(response) {
    }, function errorCallback(response) {
    });

    $scope.doLogout = function () {
        window.location.href = "/logout"
    }
}
